import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;

public class MatchUpScreen {
	// Create a new JFrame with a title of "Coverage Corner"
	JFrame matchupFrame = new JFrame("Coverage Corner - Matchup Screen");
	RecentScore[] recentScores;
	MatchUp[] matchUPS;
	League league;
	Team TeamA;
	Team TeamB;
	String aData[][] = new String[10][2];
	String bData[][] = new String[10][2];
	String col[] = new String[2];
	
	public MatchUpScreen() {
		
	}
	
	public void runMatchUpScreen(League L, MatchUp[] matchup, RecentScore[] recent, Team teamA, Team teamB, int scoreA, int scoreB) {
		Toolkit tk = Toolkit.getDefaultToolkit();
		int screenWidth = ((int) tk.getScreenSize().getWidth());
		int screenHeight = ((int) tk.getScreenSize().getHeight());
		
		this.league = L;
		this.matchUPS = matchup;
		this.recentScores = recent;
		
		

		// Set background color to black
		Container con = matchupFrame.getContentPane();
		con.setBackground(Color.BLACK);
		// Make sure sizing is alright
		matchupFrame.validate();
		
		JPanel bigPanel = new JPanel();
		bigPanel.setBackground(Color.black);
		//bigPanel.setLayout(new BoxLayout(bigPanel, BoxLayout.Y_AXIS));
		bigPanel.setLayout(null);
		bigPanel.setBounds(0,0,screenWidth,screenHeight);
		
		//Create the new Panel that will list the name and record of Team A (the first team listed in the matchup).
		JPanel teamANamePanel = new JPanel();
		//Set the color to gray
		teamANamePanel.setBackground(Color.gray);
		//New box layout, tho it doesn't really need it
		teamANamePanel.setLayout(new BoxLayout(teamANamePanel, BoxLayout.Y_AXIS));
		//Hard code the position of the panel to the top left corner
		teamANamePanel.setBounds(0, 0, (int)screenWidth/4, (int)screenHeight/5);
		
		//Create the labal with team name and record, -> set the new font, -> add the label
		JLabel newLBL = new JLabel(teamA.GetTeamName() + "  (" + teamA.GetRecord() +")");
		Font newLB = new Font(newLBL.getFont().getName(),newLBL.getFont().getStyle(),(int)bigPanel.getWidth()/2/20);
		newLBL.setFont(newLB);
		teamANamePanel.add(newLBL);
		
		//This will be the panel to hold all of the team players' info
		JPanel teamAPanel = new JPanel();
		//New bosLayout so that the players will be added, one on top of another, -> change color to lightGray for the time being.
		teamAPanel.setLayout(new BoxLayout(teamAPanel, BoxLayout.Y_AXIS));
		teamAPanel.setBackground(Color.lightGray);
		//Hardcode the next position to be right under the previous panel
		teamAPanel.setBounds(0, (int)screenHeight/4, screenWidth/4,screenHeight/2);
		
		//Need the player infos of both teams.
		Iterator<TeamMem> ateam = teamA.GetTeam();
		Iterator<TeamMem> bteam = teamB.GetTeam();
		
		//Get all of the players and their scores for the season.
		int cnt = 0;
		while (ateam.hasNext()) {
			TeamMem player = ateam.next();
			aData[cnt][0] = player.GetLastName();
			aData[cnt][1] = Float.toString(player.getProjectedPoints());
			cnt++;
			/*//Get the next player
			TeamMem player = ateam.next();
			//Display name and season points
			JLabel aP = new JLabel(player.GetLastName() + "        " + player.getProjectedPoints());
			//Change the font, -> set the new font
			Font newAP = new Font(aP.getFont().getName(),aP.getFont().getStyle(),(int)bigPanel.getWidth()/2/30);
			aP.setFont(newAP);
			//Add the player to the panel.
			teamAPanel.add(aP);*/
		}
		DefaultTableModel model= new DefaultTableModel(aData,col);
		JTable aTable = new JTable(model);
		aTable.setBackground(Color.lightGray);
		aTable.setRowHeight(30);
		aTable.getColumnModel().getColumn(0).setPreferredWidth(180);
		Font newF = new Font("rankTable", Font.BOLD,(int)bigPanel.getWidth()/60);
		aTable.setFont(newF);
		aTable.setShowGrid(false);
		teamAPanel.add(aTable);
		
		//Just Displays the bench, which currently has no players.
		JPanel teamABench = new JPanel();
		teamABench.setLayout(new BoxLayout(teamABench, BoxLayout.Y_AXIS));
		teamABench.setBackground(Color.gray);
		JLabel bench = new JLabel("Bench");
		Font newBench = new Font(bench.getFont().getName(),bench.getFont().getStyle(),(int)bigPanel.getWidth()/2/30);
		bench.setFont(newBench);
		teamABench.setBounds(0, screenHeight-screenHeight/4,screenWidth/4,screenHeight/5);
		teamABench.add(bench);
		
		//Create the new Panel that will list the name and record of Team B (the second team listed in the matchup).
		JPanel teamBNamePanel = new JPanel();
		teamBNamePanel.setBackground(Color.gray);
		teamBNamePanel.setLayout(new BoxLayout(teamBNamePanel, BoxLayout.Y_AXIS));
		//Hard code the panel to be in the top right corner
		teamBNamePanel.setBounds(screenWidth - screenWidth/4, 0, (int)screenWidth/4, (int)screenHeight/5);
		
		//Add the team name and record.
		JLabel newLBLB = new JLabel(teamB.GetTeamName() + "  (" + teamB.GetRecord() +")");
		Font newLBB = new Font(newLBLB.getFont().getName(),newLBLB.getFont().getStyle(),(int)bigPanel.getWidth()/2/20);
		newLBLB.setFont(newLBB);
		teamBNamePanel.add(newLBLB);
		
		//Create the new panel that will contain all of the players of Team B.
		JPanel teamBPanel = new JPanel();
		teamBPanel.setLayout(new BoxLayout(teamBPanel, BoxLayout.Y_AXIS));
		teamBPanel.setBackground(Color.lightGray);
		//Hard code the panel to be underneath the team name/record panel.
		teamBPanel.setBounds(screenWidth-screenWidth/4, (int)screenHeight/4, screenWidth/4,screenHeight/2);
		
		//Get all of the players for Team B
		cnt = 0;
		while (bteam.hasNext()) {
			TeamMem player = bteam.next();
			bData[cnt][0] = player.GetLastName();
			bData[cnt][1] = Float.toString(player.getProjectedPoints());
			cnt++;
			/*//Get the next player for team B
			TeamMem player = bteam.next();
			//Add the player name and season points to the label, -> set the font size
			JLabel aP = new JLabel(player.GetLastName() + "        " + player.getProjectedPoints());
			Font newAP = new Font(aP.getFont().getName(),aP.getFont().getStyle(),(int)bigPanel.getWidth()/2/30);
			aP.setFont(newAP);
			//Add the player to the panel.
			teamBPanel.add(aP);*/
		}
		DefaultTableModel mod= new DefaultTableModel(bData,col);
		JTable bTable = new JTable(mod);
		bTable.setBackground(Color.lightGray);
		bTable.setRowHeight(30);
		bTable.getColumnModel().getColumn(0).setPreferredWidth(180);
		bTable.setFont(newF);
		bTable.setShowGrid(false);
		teamBPanel.add(bTable);
		
		//Just Displays the bench, which currently has no players.
		JPanel teamBBench = new JPanel();
		teamBBench.setLayout(new BoxLayout(teamBBench, BoxLayout.Y_AXIS));
		teamBBench.setBackground(Color.gray);
		JLabel benchB = new JLabel("Bench");
		Font newBenchB = new Font(benchB.getFont().getName(),benchB.getFont().getStyle(),(int)bigPanel.getWidth()/2/30);
		benchB.setFont(newBenchB);
		teamBBench.setBounds(screenWidth-screenWidth/4, screenHeight-screenHeight/4,screenWidth/4,screenHeight/5);
		teamBBench.add(benchB);
		
		//This will be the panel that will maybe hold the stats in the middle of the screen.
		JPanel statsPanel = new JPanel();
		statsPanel.setBackground(Color.green);
		statsPanel.setBounds((screenWidth/4)+5, screenHeight/4, (screenWidth/2)-10, screenHeight/2);
		
		//This Panel holds all of the scores.
		JPanel scoresPanel = new JPanel();
		scoresPanel.setBackground(Color.white);
		scoresPanel.setBounds((screenWidth/2) - (screenWidth/16), 0, screenWidth/8, screenHeight/8);
		int aScore = scoreA;
		int bScore = scoreB;
		JLabel scoreLabel = new JLabel(aScore + "    " + bScore);
		Font newscores = new Font(scoreLabel.getFont().getName(),scoreLabel.getFont().getStyle(),(int)bigPanel.getWidth()/2/30);
		scoreLabel.setFont(newscores);
		scoresPanel.add(scoreLabel);
		
		JPanel vsPanel = new JPanel();
		vsPanel.setLayout(new BoxLayout(vsPanel, BoxLayout.Y_AXIS));
		vsPanel.setBackground(Color.DARK_GRAY);
		vsPanel.setBounds((screenWidth/2)-(screenWidth/16), screenHeight-(screenHeight/5), screenWidth/8, screenHeight/8);
		vsPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		vsPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
		
		JLabel teamALabel = new JLabel(teamA.GetTeamName() + " (" + teamA.GetRecord()+")");
		Font afont = new Font(teamALabel.getFont().getName(),teamALabel.getFont().getStyle(),(int)bigPanel.getWidth()/2/50);
		teamALabel.setForeground(Color.white);
		teamALabel.setFont(afont);
		JLabel vs = new JLabel("vs");
		Font vsfont = new Font(vs.getFont().getName(),vs.getFont().getStyle(),(int)bigPanel.getWidth()/2/50);
		vs.setFont(vsfont);
		vs.setForeground(Color.white);
		JLabel teamBLabel = new JLabel(teamB.GetTeamName() + " (" + teamB.GetRecord() + ")");
		Font bfont = new Font(teamBLabel.getFont().getName(),teamBLabel.getFont().getStyle(),(int)bigPanel.getWidth()/2/50);
		teamBLabel.setFont(bfont);
		teamBLabel.setForeground(Color.white);
		vsPanel.add(teamALabel);
		vsPanel.add(vs);
		vsPanel.add(teamBLabel);
		
		MouseListener MUListener = new MouseAdapter() {
			  public void mouseClicked(MouseEvent event) {
			    matchupFrame.setVisible(false); // figure out the source
			    MainScreen ms = new MainScreen();
			    ms.runMainScreen(league, matchUPS,recentScores); // display the text in the clicked label
			    // message is a JLabel
			  }
			};
		
			//Add the mouseListener to go back to the main screen to all panels and the big panel.
		teamANamePanel.addMouseListener(MUListener);
		teamAPanel.addMouseListener(MUListener);
		teamABench.addMouseListener(MUListener);
		teamBNamePanel.addMouseListener(MUListener);
		teamBPanel.addMouseListener(MUListener);
		teamBBench.addMouseListener(MUListener);
		statsPanel.addMouseListener(MUListener);
		vsPanel.addMouseListener(MUListener);
		aTable.addMouseListener(MUListener);
		bTable.addMouseListener(MUListener);

		bigPanel.addMouseListener(MUListener);
		
		//Add all panels to the big panel, which will be displayed.
		bigPanel.add(teamANamePanel);
		bigPanel.add(Box.createRigidArea(new Dimension(0, 10)));
		bigPanel.add(teamAPanel);
		bigPanel.add(teamABench);
		bigPanel.add(teamBNamePanel);
		bigPanel.add(teamBPanel);
		bigPanel.add(teamBBench);
		bigPanel.add(statsPanel);
		bigPanel.add(scoresPanel);
		bigPanel.add(vsPanel);
		bigPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		//Add the big panel to the matchup frame.
		matchupFrame.add(bigPanel);
		// Pack it all up
		matchupFrame.pack();

		// Set the default close operation to eit when you click the X button
		matchupFrame.setDefaultCloseOperation(matchupFrame.EXIT_ON_CLOSE);
		// Center the JFrame
		matchupFrame.setLocationRelativeTo(null);
		
		// Set the window to full screen mode
		matchupFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		// Set the visibility
		matchupFrame.setVisible(true);
	}
}
